In Java, an interface is even more "abstract" than an abstract class. An interface has no constructors or instance variables and no code at all, just headings for methods. All its methods are public and abstract.
public interface Fillable
{
void fill (int x);
int getCurrentAmount();
int getmaximum Capacity();
}
A class "implements" an interface by supplying code for all the interface methods.
implements is a reserved word.
For Example :
public class Car implements Fillable
{
public void fill (int gallons) {fuel amount += gallons};
public int getCurrentCapacity() {return fuelAmount};
public int getMaximumCapacity() {return fuelTankCapacity};
}
Comparable interface:
The comparable interface does not require an equals method to compile, but it is better to override the equals method inherited from Object and make it consistent with compareTo, avoid possible errors later.
For example:
public boolean equals(Object other)
{
return other != null && compareTo(other) == 0;
}
public interface Fillable
{
void fill (int x);
int getCurrentAmount();
int getmaximum Capacity();
}
A class "implements" an interface by supplying code for all the interface methods.
implements is a reserved word.
For Example :
public class Car implements Fillable
{
public void fill (int gallons) {fuel amount += gallons};
public int getCurrentCapacity() {return fuelAmount};
public int getMaximumCapacity() {return fuelTankCapacity};
}
Comparable interface:
The comparable interface does not require an equals method to compile, but it is better to override the equals method inherited from Object and make it consistent with compareTo, avoid possible errors later.
For example:
public boolean equals(Object other)
{
return other != null && compareTo(other) == 0;
}